public class fva_ctlr 
{
    Public Health_Card_Renewal_Form__c Application {get; set;}
    
    Public String visaIssueDate {get; set;}
    Public String dateOfBirth {get; set;}
    Public String passportIssueDate {get; set;}    
    Public String passportExpiryDate {get; set;}    
    Public String companyDate {get; set;}   
    Public String expectedDateOfArrival {get; set;}   
    
    public fva_ctlr()
    {
        Application = new Health_Card_Renewal_Form__c();
        Application.First_Name__c = 'First';
        Application.Middle_Name__c = 'Middle';
        Application.Last_Name__c = 'Last';
    }
    
    public List<SelectOption> getCountries()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Country__c> CountriesList = [SELECT id, Name FROM Country__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Country__c country : CountriesList)
        {
            options.add(new SelectOption(country.id, country.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getCountriesList()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<CountriesList__c> CountriesList = [SELECT id, Name FROM CountriesList__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(CountriesList__c country : CountriesList)
        {
            options.add(new SelectOption(country.id, country.name));
        }
        
        return options;
    }
    public List<SelectOption> getFVAVisitingCountry()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<CountriesList__c> CountriesList = [SELECT id, Name FROM CountriesList__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(CountriesList__c country : CountriesList)
        {
            options.add(new SelectOption(country.id, country.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getCompanies()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Account> CompaniesList = [SELECT id, Name FROM Account Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Account company : CompaniesList)
        {
            options.add(new SelectOption(company.id, company.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getJobTitles()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Occupation__c> OccupationsList = [SELECT id, Name FROM Occupation__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Occupation__c occupation : OccupationsList)
        {
            options.add(new SelectOption(occupation.id, occupation.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getQualifications()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Qualification__c> QualificationsList = [SELECT id, Name FROM Qualification__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Qualification__c qualification : QualificationsList)
        {
            options.add(new SelectOption(qualification.id, qualification.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getGenders()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Male', 'Male'));
        options.add(new SelectOption('Female', 'Female'));
        
        return options;
    }
    
    public List<SelectOption> getTitles()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Mr', 'Mr'));
        options.add(new SelectOption('Mrs', 'Mrs'));
        options.add(new SelectOption('Ms', 'Ms'));
        
        return options;
    }
    
    public List<SelectOption> getMaritalStatuses()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Single', 'Single'));
        options.add(new SelectOption('Married', 'Married'));
        options.add(new SelectOption('Divorced', 'Divorced'));
        options.add(new SelectOption('Widowed', 'Widowed'));
        
        return options;
    }
    
    public List<SelectOption> getReligions()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('', ''));
        options.add(new SelectOption('Bahaei', 'Bahaei'));
        options.add(new SelectOption('Budhist', 'Budhist'));
        options.add(new SelectOption('Cristian', 'Cristian'));
        options.add(new SelectOption('Hindu', 'Hindu'));
        options.add(new SelectOption('Kadiani', 'Kadiani'));
        options.add(new SelectOption('Muslim', 'Muslim'));
        options.add(new SelectOption('Sikh', 'Sikh'));
        
        return options;
    }
    
    Public PageReference CreateApplication()
    {
        if (Application.Visa_Number__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Visa Number'));
        }
        if (visaIssueDate== null || visaIssueDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Visa Issue Date'));
        }else{
           Application.Visa_Issue_Date__c = stringToDate(visaIssueDate);
        }
        
        
        if (Application.Title__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Title'));
        }
        
        if (Application.First_Name__c == 'First' || Application.First_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for First Name'));
        }
        if (Application.Middle_Name__c == 'Middle'){          
           Application.Middle_Name__c = '' ;
        }
        if (Application.Last_Name__c == 'Last' || Application.Last_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Last Name'));
        }       
        if (Application.Currently_Nationality__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Currently Nationality'));
        }
        if (Application.Previous_Nationality__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Previous Nationality'));
        }
        
        if (dateOfBirth== null || dateOfBirth==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Date of Birth'));
        }else{
           Application.Date_of_Birth__c = stringToDate(dateOfBirth);
        }
        
        if (Application.Country_of_Birth__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Country of Birth'));
        }
        
        if (Application.City_of_Birth__c == null || Application.City_of_Birth__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose City of Birth'));
        }
        
        if (Application.Passport_Number__c == null || Application.Passport_Number__c == '' ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Passport Number'));
        }
        
        if (passportIssueDate == null || passportIssueDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Issue Date'));
        }else{
           Application.Passport_Issue_Date__c = stringToDate(passportIssueDate);
        }
        
        if (passportExpiryDate == null || passportExpiryDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Expiry Date'));
        }else{
           Application.Passport_Expiry_Date__c = stringToDate(passportExpiryDate);
        }
        
        if (Application.Passport_Country_of_Issue__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Country of Issue'));
        }
        
        if (Application.Passport_City_of_Issue__c == null || Application.Passport_City_of_Issue__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport City of Issue'));
        }
        
        if (Application.Mother_Full_Name__c == null || Application.Mother_Full_Name__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Mother Full Name'));
        }
        
        if (Application.Marital_Status__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Marital Status'));
        }
        
        if (Application.Religion__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Religion'));
        }
        
        if (Application.Job_Title_Position__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Job Title/Position'));
        }
        
        if (Application.Education_Qualification__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Education Qualification'));
        }
        
        if (Application.Gender__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Gender'));
        }
        
        if (Application.Company_Name__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose a Company'));
        }
        if (Application.Name_of_Authorized_Signatory__c == null || Application.Name_of_Authorized_Signatory__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Name of Authorized Signatory'));
        }        
        
        if (companyDate == null || companyDate == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Company Date'));
        }else{
           Application.Date__c = stringToDate(companyDate);
        }
        
        if (Application.FVA_Visiting_Country__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Visiting Country'));
        }
        
        if (expectedDateOfArrival == null || expectedDateOfArrival == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Expected Date of Arrival'));
        }else{
           Application.FVA_Expected_Date_of_Arrival__c = stringToDate(expectedDateOfArrival);
        }
        
        if (Application.FVA_Duration_of_Visit__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Duration of Visit'));
        }
        
        if (Application.FVA_Purpose_of_Visit__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Purpose of Visit'));
        }
    
        
        if (ApexPages.hasMessages()){
           return null ;
        }
    
        try
        {
            insert(Application);
            
            PageReference pg = new PageReference('/apex/fvaPDF?id=' + Application.id);
            return pg;
        }
        catch(System.Exception Exp)
        {
            ApexPages.AddMessages(Exp);
            return null;
        }
    }
    
    Public PageReference ResetApplication()
    {
        Application = new Health_Card_Renewal_Form__c();
        
        return null;
    }
    
    Public PageReference ToMainMenu()
    {
        PageReference pg = new PageReference('/home');
        return pg;
    }
    
    public static Date stringToDate(String s){
      //XML Date String is in the format dd/mm/yyyy
      String[] stringDate = s.split('-');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
    }
}